package cn.xfdzx.android.apps.shop.presenter;

import android.util.Log;

import java.util.Map;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.ConfirmGoodBeen;
import cn.xfdzx.android.apps.shop.bean.MoreGoodsBean;
import cn.xfdzx.android.apps.shop.bean.ShopCarListBeen;
import cn.xfdzx.android.apps.shop.bean.ShopRemakeListMarketBean;
import cn.xfdzx.android.apps.shop.bean.XFDGoodsDetailsBeen;
import cn.xfdzx.android.apps.shop.contract.XFDGoodsDetailsContract;
import cn.xfdzx.android.apps.shop.model.XFDGoodsDetailsModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;

/**
 * @author azheng
 * @date 2018/6/4.
 * Description：
 */
public class XFDGoodsDetailsPresenter extends BasePresenter<XFDGoodsDetailsContract.View> implements XFDGoodsDetailsContract.Presenter {

    private XFDGoodsDetailsContract.Model model;

    public XFDGoodsDetailsPresenter() {
        model = new XFDGoodsDetailsModel();
    }

    @Override
    public void initData(String good_id) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.initData(good_id) .compose(RxScheduler.<XFDGoodsDetailsBeen>Flo_io_main())
                .as(mView.<XFDGoodsDetailsBeen>bindAutoDispose())
                .subscribe(new Consumer<XFDGoodsDetailsBeen>() {
                    @Override
                    public void accept(XFDGoodsDetailsBeen bean) throws Exception {
                        mView.onInitDataSuccess(bean);
                        mView.hideLoading();

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void addCar(String is_new, String uniqueId, String productId, String cartNum) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.addCar(is_new,uniqueId,productId,cartNum) .compose(RxScheduler.<ConfirmGoodBeen>Flo_io_main())
                .as(mView.<ConfirmGoodBeen>bindAutoDispose())
                .subscribe(new Consumer<ConfirmGoodBeen>() {
                    @Override
                    public void accept(ConfirmGoodBeen bean) throws Exception {
                        mView.onAddCarSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void shopCarList(String store_id, String index_status) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.shopCarList(store_id,index_status)
                .compose(RxScheduler.<ShopCarListBeen>Flo_io_main())
                .as(mView.<ShopCarListBeen>bindAutoDispose())
                .subscribe(new Consumer<ShopCarListBeen>() {
                    @Override
                    public void accept(ShopCarListBeen bean) throws Exception {
                        mView.onShopCarListSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void getStoreGoodsList(Map<String, Object> map) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getStoreGoodsList(map)
                .compose(RxScheduler.<MoreGoodsBean>Flo_io_main())
                .as(mView.<MoreGoodsBean>bindAutoDispose())
                .subscribe(new Consumer<MoreGoodsBean>() {
                    @Override
                    public void accept(MoreGoodsBean bean) throws Exception {
                        mView.getStoreGoodsListSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void getGoodsCollection(Map<String, Object> map) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getGoodsCollection(map)
                .compose(RxScheduler.<MoreGoodsBean>Flo_io_main())
                .as(mView.<MoreGoodsBean>bindAutoDispose())
                .subscribe(new Consumer<MoreGoodsBean>() {
                    @Override
                    public void accept(MoreGoodsBean bean) throws Exception {
                        mView.getGoodsCollectionSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void getGoodsCancleCollection(Map<String, Object> map) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getGoodsCancleCollection(map)
                .compose(RxScheduler.<MoreGoodsBean>Flo_io_main())
                .as(mView.<MoreGoodsBean>bindAutoDispose())
                .subscribe(new Consumer<MoreGoodsBean>() {
                    @Override
                    public void accept(MoreGoodsBean bean) throws Exception {
                        mView.getGoodsCancleCollectionSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void getShopRemakeMarket(Map<String, Object> map) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
//        if (isLoading){
//            mView.showLoading();
//        }
        model.getShopRemakeMarket(map)
                .compose(RxScheduler.<ShopRemakeListMarketBean>Flo_io_main())
                .as(mView.<ShopRemakeListMarketBean>bindAutoDispose())
                .subscribe(new Consumer<ShopRemakeListMarketBean>() {
                    @Override
                    public void accept(ShopRemakeListMarketBean bean) throws Exception {
                        mView.onGetShopRemakeMarketSuccess(bean);
//                        if (isLoading){
//                            mView.hideLoading();
//                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
//                        if (isLoading){
//                            mView.hideLoading();
//                        }
//                        if (isRefresh){
//                            mView.finishRefresh();
//                        }else {
//                            mView.finishLoadMore();
//                        }
                    }
                });
    }
}
